package vip.xiaonuo.modular.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import vip.xiaonuo.modular.business.param.BiIndexData.QueryParam;
import vip.xiaonuo.modular.business.service.IndexDataManageService;
import vip.xiaonuo.modular.business.service.ThemeApiService;
import vip.xiaonuo.modular.manage.biindexdata.service.BiIndexDataService;
import vip.xiaonuo.modular.manage.poidata.param.PoiDataParam;
import vip.xiaonuo.modular.manage.poidata.service.PoiDataService;
import vip.xiaonuo.modular.manage.theme.entity.BiTheme;
import vip.xiaonuo.modular.manage.theme.param.ThemeParam;
import vip.xiaonuo.modular.manage.theme.service.ThemeIndexService;
import vip.xiaonuo.modular.manage.theme.service.ThemeService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: cdx
 * @Date:2022/8/31
 */
@Service
public class ThemeApiServiceImpl implements ThemeApiService {

    @Resource
    private ThemeService themeService;
    @Resource
    private ThemeIndexService themeIndexService;

    @Resource
    private BiIndexDataService biIndexDataService;
    @Resource
    private IndexDataManageService indexDataManageService;

    @Resource
    private PoiDataService poiDataService;


    @Override
    public BiTheme getThemeData(@RequestBody ThemeParam themeParam){
        //      主题
        BiTheme biTheme = new BiTheme();
        if (ObjectUtil.isNotNull(themeParam)){
            biTheme = themeService.getById(themeParam.getId());
//      主题对应的指标类型ids
            List<Long> indexIds = themeIndexService.findindexIds(themeParam.getId());
//      根据指标类型ids查询指标数据
            if (indexIds.size() !=0){
//                List<BiIndexData> byTypeIds = biIndexDataService.findByTypeIds(indexIds);
                QueryParam param = new QueryParam();
                BeanUtil.copyProperties(themeParam,param);
                param.setTypeIds(indexIds);
                if(StringUtils.hasText(themeParam.getYear())){
                    param.setYear(Integer.parseInt(themeParam.getYear()));
                }
                JSONArray o = indexDataManageService.queryIndexData(param);
                biTheme.setBiIndexData(o);
            }

//      查询poi数据
            List<Long> poiIds = this.themeIndexService.findPoiIds(themeParam.getId());
            if (poiIds.size() != 0 ){
                PoiDataParam poiDataParam = new PoiDataParam();
                BeanUtil.copyProperties(themeParam,poiDataParam);
                poiDataParam.setSubTagIds(poiIds);
                if(StringUtils.hasText(themeParam.getYear())){
                    poiDataParam.setYear(Integer.parseInt(themeParam.getYear()));
                }
                if(StringUtils.hasText(themeParam.getLng1())&& StringUtils.hasText(themeParam.getLng2())){
                    poiDataParam.setLng1(Double.parseDouble(themeParam.getLng1()));
                    poiDataParam.setLng2(Double.parseDouble(themeParam.getLng2()));
                    poiDataParam.setLat1(Double.parseDouble(themeParam.getLat1()));
                    poiDataParam.setLat2(Double.parseDouble(themeParam.getLat2()));
                }
                biTheme.setBiPoiData(poiDataService.customList(poiDataParam));
            }
        }
        return biTheme;
    }

}
